package server;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import phasebook.client.RemotePhasebookService;

public class ImageServlet extends HttpServlet{
	private static final long serialVersionUID = 2145692589477490290L;
	@EJB 
	RemotePhasebookService service;
	
	private RemotePhasebookService getService() {
		if (service!=null) // running in production with EJB injection
			return service;
		else //for web-development use this dummy service
			return DevPhasebookService.getInstance();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		long imageId = Long.parseLong(req.getParameter("id"));
		byte[] photo = getService().downloadPhoto(imageId);
		resp.setHeader("Cache-Control", "max-age=60");
		resp.getOutputStream().write(photo);
	}
}
